define(["jquery","bbcare","comm/form/form","bch/app/specialSchemeRegister/lapseToDetail.servers"],function($,$bb,$form,$servers){
	var lapseToDetail = {};

	/**
	 * 转归成功后的回调函数
	 */
	var ajaxSubmitTaskCallBack = function(data,params,domId){
		$bb.toastShow("转归成功！");
		var params = $bb.getPageParams();
        $bb.open(params,domId,"bch_lapseToList");
	}

	/**
	 * 转归提交
	 */
	var onSubmitClick = function(){
		if( $form.validate("archiveBox") ){
			var data = $(this).data("params");
			var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
			var contList = $form.getSaveData("lapseToDetailForm");
			var channelId = $bb.getChannelId();
	    	var params = {
	    		"oprInfo":{
	    			"opCode":"1001",
					"opType":"2",
	    			"channelId":channelId,
	    			"channelType":"2",
	    			"opNote":"【"+doctorName+"】医生填写【"+data.userName+"】患者转归任务",
	    		},
	    		"taskInfo":{
	    			"ptTaskId":data.ptTaskId,
	    			"contList":contList,
	    			"saveType":"1",
	    			"tranTime":data.tranTime   // 转归时间，如果上次没有 传空
	    		},
	    		"isReg":"isReg"  // 专案转归  多这个参数
	    	};
	    	var domId = $(this).attr("id");
	    	$servers.ajaxSubmitTask(params,ajaxSubmitTaskCallBack,domId);
	    }
	}

	/**
	 * 请求转归问卷成功后的回调函数
	 */
	var ajaxRegUserTemplCallBack2 = function(data,params){
		if( data.contList && data.contList.length > 0 ){
			$form.init({
				"id":"lapseToDetailForm",
				"questions":data.contList,
				"values":data.valueList,
			});
			$("#submitBtnWrap").html('<button id="saveBtn" class="btn btn-success">确定</button>');
	        $("#saveBtn").unbind("click");
	        $("#saveBtn").data("params",params);
	        $("#saveBtn").click(onSubmitClick);
		}
	}

	/**
	 * 请求问卷成功后的回调函数
	 */
	var ajaxRegUserTemplCallBack = function(data,params){
		if( data.contList && data.contList.length > 0 ){
			$form.init({
				"id":"lapseToDetailInfo",
				"status": "readonly",
				"questions":data.contList,
				"values":data.valueList,
			});
		}
	}

	/**
	 * 查询任务成功后的回调函数
	 */
	var ajaxTaskssCallBack = function(data,params){
		if( data.rows && data.rows.length > 0 ){
			var arr1 = [], arr2 = [];
			for (var i = 0; i < data.rows.length; i++) {
				if( data.rows[i].busiType != "A0801" ){
					arr1.push(data.rows[i]);
				}else{
					arr2.push(data.rows[i]);
				}
			}
			// 若以后建档配置了多个任务，需再调整
			if( arr1.length == 1 ){
				$.extend(params,{"ptTaskId":arr1[0].id});
				$servers.ajaxTaskContTempl(params,ajaxRegUserTemplCallBack);
			}
			if( arr2.length == 1 ){
				$.extend(params,{"ptTaskId":arr2[0].id});
				$servers.ajaxTaskContTempl(params,ajaxRegUserTemplCallBack2);
			}
		}
	}

	/**
	 * 查询节点成功后的回调函数
	 */
	var ajaxNodesCallBack = function(data,params){
		if( data.rows && data.rows.length > 0 ){
			var ptNodeId = data.rows[0].id;
			$.extend(params,{"ptNodeId":ptNodeId});
			$servers.ajaxTasks(params,ajaxTaskssCallBack);
		}
	}

	lapseToDetail.init = function(){
		var params = $bb.getPageParams();
		if( params.title ) $("#lapseToDetailTit").html(params.title + "转归");
		$servers.ajaxNodes(params,ajaxNodesCallBack);
	}

	return lapseToDetail;
});